package com.java.jvalidator.framework.validator;

import java.util.ArrayList;
import java.util.List;

import com.java.jvalidator.framework.ErrorMsg;
import com.java.jvalidator.framework.TypeUtil;
import com.java.jvalidator.framework.ValidationException;
import com.java.jvalidator.framework.config.Parameters;

public abstract class AbstractCustomValidator extends AbstractValidator {
	private List<ErrorMsg> errorMsgs = new ArrayList<ErrorMsg>();

	public List<ErrorMsg> getErrorMsg() {
		return this.errorMsgs;
	}

	public void clearErrorMsg() {
		// this.errorMsgs = new ArrayList<ErrorMsg>();
		this.errorMsgs.clear();
	}

	protected void addErrorMsg(String errorMsg, String[] placeHolders) {
		ErrorMsg msg = new ErrorMsg(this.target, this.propertyName, errorMsg);
		msg.setPlaceHolderValue(placeHolders);
		this.errorMsgs.add(msg);
	}

	protected boolean beginValidate(Object target, String propertyName, Parameters param) throws ValidationException {
		clearErrorMsg();

		Object property = TypeUtil.getPropertyValue(target, propertyName);
		if (property == null) {
			return true;
		}
		if ((!isValidClassType(target)) || (!isValidPropertyType(property))) {
			throw new ValidationException("Parameter target or field type is invalid.");
		}
		return processValidate(target, propertyName, property, param);
	}

	protected boolean isValidClassType(Object target) {
		return true;
	}

	protected boolean isValidPropertyType(Object prop) {
		return prop instanceof String;
	}

	protected abstract boolean processValidate(Object paramObject1, String paramString, Object paramObject2, Parameters paramParameters) throws ValidationException;
}